package cn.tedu.mm.front.account.controller;

import cn.tedu.mm.common.validation.account.UserRules;
import cn.tedu.mm.common.web.JsonResult;
import cn.tedu.mm.front.account.Service.IUserInfoService;
import cn.tedu.mm.front.account.pojo.param.UserUpdateInfoParam;
import cn.tedu.mm.front.account.pojo.vo.UserInfoVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Pattern;

@RestController
@RequestMapping("/users/")
@Slf4j
@Validated
@Api(tags = "01. 用户信息")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    @GetMapping("info")
    @ApiOperation("获取用户信息")
    @ApiOperationSupport(order = 400)
    public JsonResult getUserInfo(Long id){
        final UserInfoVO userInfo = userInfoService.getUserInfo(id);
        return JsonResult.ok(userInfo);
    }

    @PostMapping("update/password")
    @ApiOperation("修改密码")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPassword", value = "原密码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, paramType = "query")
    })
    public JsonResult updatePassword(Long id, @Pattern(regexp = UserRules.PATTERN_PASSWORD, message = UserRules.MESSAGE_PASSWORD_PATTERN) String oldPassword,
                                     @Pattern(regexp = UserRules.PATTERN_PASSWORD, message = UserRules.MESSAGE_PASSWORD_PATTERN) String newPassword){
        userInfoService.updatePassword(id,oldPassword,newPassword);
        return JsonResult.ok();
    }
    @PostMapping("update/info")
    @ApiOperation("修改用户信息")
    @ApiOperationSupport(order = 310)
    public JsonResult updateInfo(@Validated UserUpdateInfoParam userUpdateInfoParam){
        userInfoService.updateInfo(userUpdateInfoParam);
        return JsonResult.ok();
    }
}
